<template>
    <el-aside width="220px" class="image-aside" v-loading="loading">
        <div class="top">
            <block v-for="(item, index) in list">
                <AsideList :active="activeId == item.id"  v-if="item.type == 0" :key="index" @edit="handleEdit(item)"
                @delete="handleDelete(item.id)" @click="handleChangeActiveId(item.id)">
                {{ item.name }}
            </AsideList>
            </block>
        </div>
        <div class="bottom">
            <el-pagination background layout="prev, next" :total="total" :current-page="currentPage"
                :page-size="pageSize" @current-change="getData" />
        </div>
    </el-aside>

    <FormDrawer :title="drawerTitle" ref="formDrawerRef" @submit="handleSubmit">
        <el-form :model="form" ref="formRef" :rules="rules" label-width="80px" :inline="false">
            <el-form-item label="分类名称" prop="name">
                <el-input v-model="form.name"></el-input>
            </el-form-item>
            <el-form-item label="排序" prop="order">
                <el-input-number v-model="form.order" :min="0" :max="1000" />
            </el-form-item>
        </el-form>
    </FormDrawer>
</template>
<script setup>
import { reactive, ref } from "vue";
import FormDrawer from "./FormDrawer.vue";
import {
    getImageLabelList,
    postImageLabel,
    patchImageLabel,
    deleteImageLabel,
} from "~/api/medias/image.js";
import AsideList from "./AsideList.vue";
import { toast } from "~/composables/util.js";
import { computed } from "@vue/reactivity";
import { useStore } from "vuex";

const store = useStore();

// 加载动画
const loading = ref(false);
// 标签数据
const list = ref([]);
// 选中图库分类ID
const activeId = ref(0);
// 抽屉组件
const formDrawerRef = ref(null);
// 编辑
const emit = defineEmits(["change"]);
const editId = ref(0);
const drawerTitle = computed(() => (editId.value ? "修改" : "新增"));

// 上传表单
const form = reactive({
    name: "",
    type: '0',
    order: 50,
});
// 表单内容
const formRef = ref(null);
// 上传验证
const rules = {
    name: [
        {
            required: true,
            message: "图库分类名称不能为空",
            trigger: "blur",
        },
    ],
};

// 分页
const currentPage = ref(1)
const total = ref(0)
// const pageSize = ref(store.state.pageSize.ImageAside);
const pageSize = ref(10)

// 获取数据
function getData(p = null) {
    if (typeof p == "number") {
        currentPage.value = p;
    }
    loading.value = true;
    getImageLabelList(currentPage.value, pageSize.value)
        .then((res) => {
            total.value = res.data.count;
            list.value = res.data.results;
            let item = res.data.results[0];
            if (item) {
                handleChangeActiveId(item.id);
            }

        })
        .finally(() => {
            loading.value = false;
        });
}

getData(1);

const handleSubmit = () => {
    formRef.value.validate((valid) => {
        if (!valid) return;
        formDrawerRef.value.showLoading();
        const fun = editId.value
            ? patchImageLabel(editId.value, {
                name: form.name,
                order: form.order,
            })
            : postImageLabel({
                name: form.name,
                order: form.order,
                type_name: "图片库",
            });
        fun
            .then((res) => {
                toast(drawerTitle.value + "成功");
                getData(editId.value ? currentPage.value : 1);
                formDrawerRef.value.close();
            })
            .finally(() => {
                formDrawerRef.value.hideLoading();
            });
    });
};

// 新增
const handleCreate = () => {
    editId.value = 0;
    form.name = "";
    form.order = 50;
    formDrawerRef.value.open();
};

// 编辑
const handleEdit = (row) => {
    editId.value = row.id;
    form.name = row.name;
    form.order = row.order;
    formDrawerRef.value.open();
};

// 删除
const handleDelete = (id) => {
    loading.value = true;
    deleteImageLabel(id)
        .then((res) => {
            toast("删除成功");
            getData();
        })
        .finally(() => {
            loading.value = false;
        });
};

// 切换分类
function handleChangeActiveId(id) {
    activeId.value = id;
    emit("change", id);
}

defineExpose({
    handleCreate,
});
</script>
<style>
.image-aside {
    border-right: 1px solid #eeeeee;
    position: relative;
}

.image-aside .top {
    position: absolute;
    top: 0;
    right: 0;
    left: 0;
    bottom: 50px;
    overflow-y: auto;
}

.image-aside .bottom {
    position: absolute;
    bottom: 0;
    height: 50px;
    left: 0;
    right: 0;
    @apply flex items-center justify-center;
}
</style>